#include "GY_ConcatWStrings.h"
#include <QDebug>

GY_ConcatWStrings::GY_ConcatWStrings(const QString &guid, QGraphicsItem *parent)
    :GY_BasicsCalculators(guid,this,parent)
{
    //图元类型
    GY_Object::setUserData(GY_ENUM::ClassType,QVariant("ConcatWStrings"));
    initializationPropertyInfo();
}

void GY_ConcatWStrings::initializationPropertyInfo()
{
    GY_PropertyInfo ObjectName(this);
    ObjectName.propertyName = "ObjectName";
    ObjectName.propertyValue = QString();
    ObjectName.updateType = Update;
    ObjectName.propertyType = QStringPropertyType;
    ObjectName.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_ConcatWStrings::updateObjectName;
    m_PropertyInfoMap.insert(ObjectName.propertyName,ObjectName);
    m_PropertyInfoMap[ObjectName.propertyName].setConnect();

    GY_PropertyInfo String1CapacityCapacity(this);
    String1CapacityCapacity.propertyName = "String1.Capacity";
    String1CapacityCapacity.propertyValue = 0;
    String1CapacityCapacity.updateType = Update;
    String1CapacityCapacity.propertyType = IntPropertyType;
    String1CapacityCapacity.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_ConcatWStrings::updateString1Capacity;
    m_PropertyInfoMap.insert(String1CapacityCapacity.propertyName,String1CapacityCapacity);
    m_PropertyInfoMap[String1CapacityCapacity.propertyName].setConnect();

    GY_PropertyInfo String1(this);
    String1.propertyName = "String1";
    String1.propertyValue = "";
    String1.updateType = Update;
    String1.propertyType = QStringPropertyType;
    String1.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_ConcatWStrings::updateString1;
    m_PropertyInfoMap.insert(String1.propertyName,String1);
    m_PropertyInfoMap[String1.propertyName].setConnect();

    GY_PropertyInfo String2Capacity(this);
    String2Capacity.propertyName = "String2.Capacity";
    String2Capacity.propertyValue = 0;
    String2Capacity.updateType = Update;
    String2Capacity.propertyType = IntPropertyType;
    String2Capacity.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_ConcatWStrings::updateString2Capacity;
    m_PropertyInfoMap.insert(String2Capacity.propertyName,String2Capacity);
    m_PropertyInfoMap[String2Capacity.propertyName].setConnect();

    GY_PropertyInfo String2(this);
    String2.propertyName = "String2";
    String2.propertyValue = "";
    String2.updateType = Update;
    String2.propertyType = QStringPropertyType;
    String2.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_ConcatWStrings::updateString2;
    m_PropertyInfoMap.insert(String2.propertyName,String2);
    m_PropertyInfoMap[String2.propertyName].setConnect();

    GY_PropertyInfo OutputCapacity(this);
    OutputCapacity.propertyName = "Output.Capacity";
    OutputCapacity.propertyValue = 0;
    OutputCapacity.updateType = Update;
    OutputCapacity.propertyType = IntPropertyType;
    OutputCapacity.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_ConcatWStrings::updateOutputCapacity;
    m_PropertyInfoMap.insert(OutputCapacity.propertyName,OutputCapacity);
    m_PropertyInfoMap[OutputCapacity.propertyName].setConnect();

    GY_PropertyInfo Output(this);
    Output.propertyName = "Output";
    Output.propertyValue = "";
    Output.updateType = Update;
    Output.propertyType = QStringPropertyType;
    Output.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_ConcatWStrings::updateOutput;
    m_PropertyInfoMap.insert(Output.propertyName,Output);
    m_PropertyInfoMap[Output.propertyName].setConnect();
}

bool GY_ConcatWStrings::updateString1Capacity(const QString &name, const QVariant &value)
{
    if(!value.canConvert<int>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    QString string1 = m_PropertyInfoMap["String1"].propertyValue.toString();
    if(string1.length()>value.toInt()) {
        m_PropertyInfoMap["String1"].propertyValue = string1.left(value.toInt());
    }
    setOutput();
    return true;
}

bool GY_ConcatWStrings::updateString1(const QString &name, const QVariant &value)
{
    if(!value.canConvert<QString>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    int length1 = m_PropertyInfoMap["String1.Capacity"].propertyValue.toInt();
    if(length1 < value.toString().length()) {
        m_PropertyInfoMap["String1.Capacity"].propertyValue = value.toString().length();
    }
    setOutput();
    return true;
}

bool GY_ConcatWStrings::updateString2Capacity(const QString &name, const QVariant &value)
{
    if(!value.canConvert<int>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    QString string2 = m_PropertyInfoMap["String2"].propertyValue.toString();
    if(string2.length()>value.toInt()) {
        m_PropertyInfoMap["String2"].propertyValue = string2.left(value.toInt());
    }
    setOutput();
    return true;
}

bool GY_ConcatWStrings::updateString2(const QString &name, const QVariant &value)
{
    if(!value.canConvert<QString>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    int length2 = m_PropertyInfoMap["String2.Capacity"].propertyValue.toInt();
    if(length2 < value.toString().length()) {
        m_PropertyInfoMap["String2.Capacity"].propertyValue = value.toString().length();
    }
    setOutput();
    return true;
}

bool GY_ConcatWStrings::updateOutputCapacity(const QString &, const QVariant &)
{
    return false;
}

bool GY_ConcatWStrings::updateOutput(const QString &, const QVariant &)
{
    return false;
}

void GY_ConcatWStrings::setOutput()
{
    m_PropertyInfoMap["Output.Capacity"].propertyValue = m_PropertyInfoMap["String1.Capacity"].propertyValue.toInt() + m_PropertyInfoMap["String2.Capacity"].propertyValue.toInt();
    m_PropertyInfoMap["Output"].propertyValue = m_PropertyInfoMap["String1"].propertyValue.toString() + m_PropertyInfoMap["String2"].propertyValue.toString();
//    qDebug()<<m_PropertyInfoMap["Output.Capacity"].propertyValue<<m_PropertyInfoMap["Output"].propertyValue;
}

